package com.mrd.gtimp.business.action;

import com.abc.trustpay.client.ebus.PaymentResult;
import com.mrd.exception.QueryException;
import com.mrd.exception.SaveException;
import com.mrd.gtimp.business.entity.DeclarationFree;
import com.mrd.gtimp.business.service.DeclarationBiz;
import com.mrd.gtimp.business.service.DeclarationFreeBiz;
import com.mrd.gtimp.business.service.FundFlowBiz;
import com.mrd.gtimp.business.service.PaymentABCBiz;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * @author xiewl 对接部分农业银行支付信息接口
 */

@Controller
@RequestMapping("/business")
public class PayAbcAction {

	private static final long		serialVersionUID	= 7860265128022079884L;

	Logger							log					= Logger.getLogger(PayAbcAction.class);

	@Resource
	private DeclarationBiz			declaBiz;
	@Resource
	private PaymentABCBiz			paymentABCBiz;																																																																								                                           // 农行支付接口
	@Resource
	private DeclarationFreeBiz		declarationFreeBiz;
	@Resource
	private DeclarationAction		dAction;

	private String					id					= "";
	private String					msg;
	private String					declareStr;
	private String					pay_id;
	private String					payFree;
	private String					payMent;
	private String					MSG;
	private PaymentResult			paymentResult;
	private List<String>			declaration_ids;
	private String					paymentType;
	private List<DeclarationFree>	declarationFrees;

	/**
	 * 根据declaration_id 获取支付id
	 */
	@RequestMapping("/payAbcAction-getPayId")
	@ResponseBody
	public String getPayId() {
		String hql = "from com.mrd.gtimp.business.entity.DeclarationFree free where free.declaration.id = '"
		        + declareStr + "'";
		declarationFrees = new ArrayList<DeclarationFree>();
		declarationFrees = declarationFreeBiz.findEntityByHql(hql, 0, 100, null);
		if(declarationFrees.size() > 0 && declarationFrees.get(0) != null
		        && declarationFrees.get(0).getPay_id() != null
		        && !"".equals(declarationFrees.get(0).getPay_id())) {
			id = declarationFrees.get(0).getPay_id();
			msg = "success";
		} else {
			msg = "error";
		}
		return msg;
	}

	/**
	 * 定时查询订单支付状态
	 */
	@RequestMapping("/payAbcAction-queryAbcPayMentStatus")
	@ResponseBody
	public String queryAbcPayMentStatus() {
		Boolean status = paymentABCBiz.queryABCPayStatus(pay_id);
		if(status) {// 支付成功
			id = pay_id;
			msg = "success";
		} else {// 尚未支付
			msg = "failue";
		}
		return msg;
	}

	@Resource
	FundFlowBiz fundFlowBiz;

	/**
	 * 支付成功页面 沿用原有的支付成功页面 paySuccess.jsp 添加判断参数
	 */
	@RequestMapping("/payAbcAction-payAbcSuccess")
	public String payAbcSuccess(Model model,String pay_id) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("billNo", pay_id);
		int billCount = 0;
		try {
			billCount = fundFlowBiz
			        .findEntityCountBySql("select 1 from T_BIZ_FUND_FLOW where BILL_NO=:billNo", parameters);
		} catch (QueryException e1) {
			e1.printStackTrace();
		}
		/**
		 * fix支付漏洞，确保银行转账已到账，或者余额扣款成功
		 */
		// if(!paymentABCBiz.queryABCPayStatus(pay_id) && billCount <= 0) {
		// try {
		// response.sendRedirect("/gtimp/business/declaration-main.do?declaraState=DS00");
		// return "redirect:/business/unpaidMain";
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// return "SUCCESS";
		// }
		try {
			String hql = "from com.mrd.gtimp.business.entity.DeclarationFree free where free.pay_id = '"
			        + pay_id + "'";
			declarationFrees = new ArrayList<DeclarationFree>();
			declarationFrees = declarationFreeBiz.findEntityByHql(hql, 0, 100, null);
			declaration_ids = new ArrayList<String>();
			if(declarationFrees != null && declarationFrees.size() > 0) {
				for(DeclarationFree free : declarationFrees) {
					free.setPay_comfirm("1");
					free.setPay_time(null);
					declarationFreeBiz.saveOrupdateEntity(free);
					declaration_ids.add(free.getDeclaration().getId());
					log.info("支付成功，订单的pay_id ==" + pay_id);
					log.info("订单的 订单号：" + free.getDeclaration().getId());
				}
				payFree = declarationFrees.get(0).getSum_free();
			}
			dAction.setDeclaration_ids(declaration_ids);
			dAction.bizConfirm(declaration_ids.toArray(new String[0]));
			model.addAttribute("declarationFrees", declarationFrees);
			model.addAttribute("payFree", payFree);
		} catch (SaveException e) {
			e.printStackTrace();
		}
		return "/gtimp/business/paySuccess";
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getDeclareStr() {
		return declareStr;
	}

	public void setDeclareStr(String declareStr) {
		this.declareStr = declareStr;
	}

	public String getPay_id() {
		return pay_id;
	}

	public void setPay_id(String pay_id) {
		this.pay_id = pay_id;
	}

	public String getPayFree() {
		return payFree;
	}

	public void setPayFree(String payFree) {
		this.payFree = payFree;
	}

	public String getPayMent() {
		return payMent;
	}

	public void setPayMent(String payMent) {
		this.payMent = payMent;
	}

	public String getMSG() {
		return MSG;
	}

	public void setMSG(String mSG) {
		MSG = mSG;
	}

	public PaymentResult getPaymentResult() {
		return paymentResult;
	}

	public void setPaymentResult(PaymentResult paymentResult) {
		this.paymentResult = paymentResult;
	}

	public List<String> getDeclaration_ids() {
		return declaration_ids;
	}

	public void setDeclaration_ids(List<String> declaration_ids) {
		this.declaration_ids = declaration_ids;
	}

	public String getPaymentType() {
		return paymentType;
	}

	public void setPaymentType(String paymentType) {
		this.paymentType = paymentType;
	}

	public List<DeclarationFree> getDeclarationFrees() {
		return declarationFrees;
	}

	public void setDeclarationFrees(List<DeclarationFree> declarationFrees) {
		this.declarationFrees = declarationFrees;
	}

}
